package com.krtech.wecard.module.ai.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoFaceResponse;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.module.ai.MergeVideoFace;
import com.krtech.wecard.module.ai.entity.mergeVideoByPicVo;
import com.krtech.wecard.module.ai.service.MergeVideoFaceService;
import com.krtech.wecard.module.pub.dto.CardInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class MergeVideoFaceServiceImpl implements MergeVideoFaceService {

    private static String accessKeyId = "LTAI5tAK8JHwnntxE5ERvhbT";
    private static String accessKeySecret = "Y0A67pO2DloUcZDaZGpJrFtriCsDsP";

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String mergeVideoByPic(mergeVideoByPicVo mergeVideoByPicVo) {
        String RequestId= UrlFace(mergeVideoByPicVo);
        if (RequestId == null){
            throw new BusinessException("本次任务错误，请连续管理员");
        }
        return RequestId;
    }

    public String UrlFace(mergeVideoByPicVo mergeVideoByPicVo) {

        try {
            // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
            // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
            // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
            com.aliyun.videoenhan20200320.Client client = MergeVideoFace.createClient(accessKeyId, accessKeySecret);
            com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest mergeVideoFaceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest()
                    .setVideoURL(mergeVideoByPicVo.getVideoURL())
                    .setReferenceURL(mergeVideoByPicVo.getReferenceURL());
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//            MergeVideoFaceResponse mergeVideoFaceResponse = client.mergeVideoFace(mergeVideoFaceRequest);
            MergeVideoFaceResponse mergeVideoFaceResponse = client.mergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime);
            // 获取整体结果
            JSONObject data = JSONUtil.parseObj(mergeVideoFaceResponse);
            String RequestId = JSONUtil.parseObj(data.get("body")).get("RequestId").toString();

            return RequestId;

        } catch (TeaException teaException) {
            teaException.printStackTrace();
            // 获取整体报错信息
            log.error(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            log.error(teaException.getCode());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String json = "{\"headers\":{\"access-control-allow-origin\":\"*\",\"date\":\"Thu, 17 Apr 2025 13:37:51 GMT\",\"content-length\":\"231\",\"keep-alive\":\"timeout=25\",\"x-acs-request-id\":\"F5A6DDB8-0BB7-5E18-9CED-2D1CA717F3F8\",\"connection\":\"keep-alive\",\"content-type\":\"application/json;charset=utf-8\",\"etag\":\"11Yyh6+wKipQPcqRjF5zToA1\",\"access-control-expose-headers\":\"*\",\"x-acs-trace-id\":\"bcd717eb6064bf8466dbb6c11c130376\"},\"body\":{\"Message\":\"该调用为异步调用，任务已提交成功，请以requestId的值作为jobId参数调用同类目下GetAsyncJobResult接口查询任务执行状态和结果。\",\"RequestId\":\"F5A6DDB8-0BB7-5E18-9CED-2D1CA717F3F8\"},\"statusCode\":200}\n" +
                "com.aliyun.videoenhan20200320.models.MergeVideoFaceResponseBody@74455848";




    }
}
